package com.example.goodtrash.util;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class HttpUtil {
    public static void sendOkHttpRequest(String text,okhttp3.Callback callback){
        OkHttpClient client=new OkHttpClient();
        long time=System.currentTimeMillis();
        String s="09eb5a1e88776caecde2b6ede76380ab"+time;
        String sign= md5util.toMd5(s);
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, "{cityId:\""+"310000"+"\",text:\""+text+"\"}");
        Request request=new Request.Builder()
                .url("https://aiapi.jd.com/jdai/garbageTextSearch?appkey=64c4ecd219aa9bc2867ebc1c776fb30a&timestamp="+time+"&sign="+sign)
                .post(body)
                .build();
        client.newCall(request).enqueue(callback);
    }

    public static void sendHotSearchRequest(okhttp3.Callback callback){
        OkHttpClient client=new OkHttpClient();
        Request request=new Request.Builder()
                .url("http://api.tianapi.com/hotlajifenlei/index?key=a6e4914e2532f7c94ec29dabb22a3ead")
                .build();
        client.newCall(request).enqueue(callback);
    }

    public static void sendImageRequest(String image,okhttp3.Callback callback){
        OkHttpClient client=new OkHttpClient();
        long time=System.currentTimeMillis();
        String s="09eb5a1e88776caecde2b6ede76380ab"+time;
        String sign= md5util.toMd5(s);
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, "{cityId:\""+"310000"+"\",imgBase64:\""+image+"\"}");
        Request request=new Request.Builder()
                .url("https://aiapi.jd.com/jdai/garbageImageSearch?appkey=64c4ecd219aa9bc2867ebc1c776fb30a&timestamp="+time+"&sign="+sign)
                .post(body)
                .build();
        client.newCall(request).enqueue(callback);
    }

    public static void sendNewsRequest(okhttp3.Callback callback){
        OkHttpClient client=new OkHttpClient();
        Request request=new Request.Builder()
                .url("http://api.tianapi.com/lajifenleinews/index?key=a6e4914e2532f7c94ec29dabb22a3ead&num=10")
                .build();
        client.newCall(request).enqueue(callback);
    }
}
